package com.example.easybuy.service.impl;

import com.example.easybuy.entity.Address;
import com.example.easybuy.mappers.AddressMapper;
import com.example.easybuy.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressMapper addressMapper;
    @Override
    public boolean addAddress(Map<String, Object> map) {
        return addressMapper.addAddress(map)>0;
    }

    @Override
    public boolean update(Map<String, Object> map) {
        return addressMapper.updateAddress(map)>0;
    }

    @Override
    public boolean delAddress(Map<String, Object> map) {
        return addressMapper.delAddress(map)>0;
    }

    @Override
    public List<Address> selectAll(Map<String, Object> map) {
        return addressMapper.selectAll(map);
    }

    @Override
    public Address findAddress(int id) {
        return addressMapper.findAddress(id);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean updateIsDefault(int id) {
        try {
            boolean toNo= addressMapper.toNoIsDefault()>0;
            boolean toUpdate=addressMapper.updateIsDefault(id)>0;
            return toUpdate&&toNo;
        }catch (Exception e){
            throw new RuntimeException("更新默认地址失败", e);
        }
    }

}
